package com.bstek.uflo.form.view.common.impl;

import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;
import org.springframework.util.StringUtils;

import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.Component;
import com.bstek.dorado.view.widget.Container;
import com.bstek.dorado.view.widget.base.toolbar.ToolBar;
import com.bstek.dorado.view.widget.datacontrol.DataPilot;
import com.bstek.dorado.view.widget.grid.DataColumn;
import com.bstek.dorado.view.widget.grid.DataGrid;
import com.bstek.uflo.form.Constants;
import com.bstek.uflo.form.model.TableDefinition;
import com.bstek.uflo.form.view.common.AbstractComponentBuilder;

/**
 * @author Jacky.gao
 * @since 2013年11月15日
 */
@org.springframework.stereotype.Component("com.bstek.uflo.form.view.common.impl.DataGridBuilder")
public class DataGridBuilder extends AbstractComponentBuilder {

	public boolean support(TagNode node) {
		if(node instanceof Div){
			Div div=(Div)node;
			String css=div.getAttribute("class");
			if(css!=null && css.equals("uflogrid")){
				return true;
			}
		}
		return false;
	}

	public void build(TagNode node, View view, Map<String, Component> map,Map<String,Boolean> mappingMap,TableDefinition table) {
		Div div=(Div)node;
		String id=div.getAttribute("id");
		if(StringUtils.isEmpty(id))return;
		if(map.containsKey(id))return;
		Container container=new Container();
		container.setRenderOn("#"+id);
		view.addChild(container);
		map.put(id, container);
		
		ToolBar toolbar=new ToolBar();
		DataPilot dp=new DataPilot();
		dp.setItemCodes("+,-");
		dp.setDataSet(Constants.DATASET);
		dp.setDataPath(Constants.SLAVE_PROPERTY);
		toolbar.addItem(dp);
		container.addChild(toolbar);
		NodeList list=div.getChildren();
		for(int i=0;i<list.size();i++){
			Node cnode=list.elementAt(i);
			if(cnode instanceof Div){
				Div cdiv=(Div)cnode;
				String css=cdiv.getAttribute("class");
				if(css!=null && css.equals("uflogridheader")){
					DataGrid grid=buildDataGrid(view,cdiv);
					container.addChild(grid);
					break;
				}
			}
		}
	}
	
	private DataGrid buildDataGrid(View view,Div div){
		DataGrid grid=new DataGrid();
		grid.setDataSet(Constants.DATASET);
		grid.setDataPath(Constants.SLAVE_PROPERTY);
		grid.setAutoCreateColumns(false);
		grid.setHeight("220");
		NodeList list=div.getChildren();
		for(int i=0;i<list.size();i++){
			Node node=list.elementAt(i);
			if(!(node instanceof Span)){
				continue;
			}
			Span span=(Span)node;
			String css=span.getAttribute("class");
			if(css==null || !css.equals("uflogridcolumn")){
				continue;
			}
			String bindingField=span.getAttribute("bindingfield");
			String caption=span.getChildrenHTML();
			grid.addColumn(buildDataColumn(bindingField.toLowerCase(),caption));
		}
		return grid;
	}
	
	private DataColumn buildDataColumn(String field,String caption){
		DataColumn col=new DataColumn();
		col.setName(field);
		col.setProperty(field);
		col.setCaption(caption);
		return col;
	}
}
